home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite / RCS / connection.c,v < prev    next >
Encoding:
Text File  |  1992-08-26  |  28.5 KB  |  1,193 lines

  1. head     1.6;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    jhh:1.6; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.6
  10. date     92.08.26.12.39.41;  author jhh;  state Exp;
  11. branches ;
  12. next     1.5;
  13.  
  14. 1.5
  15. date     92.08.20.16.25.15;  author jhh;  state Exp;
  16. branches ;
  17. next     1.4;
  18.  
  19. 1.4
  20. date     92.01.24.17.40.14;  author stolcke;  state Exp;
  21. branches ;
  22. next     1.3;
  23.  
  24. 1.3
  25. date     91.10.19.13.29.23;  author mendel;  state Exp;
  26. branches ;
  27. next     1.2;
  28.  
  29. 1.2
  30. date     90.03.10.23.24.01;  author tve;  state Exp;
  31. branches ;
  32. next     1.1;
  33.  
  34. 1.1
  35. date     90.02.14.19.25.15;  author tve;  state Exp;
  36. branches ;
  37. next     ;
  38.  
  39.  
  40. desc
  41. @Original X11R4 distribution
  42. @
  43.  
  44.  
  45. 1.6
  46. log
  47. @disabling all TCP connections is not a smart thing to do
  48. @
  49. text
  50. @/***********************************************************
  51. Copyright 1987, 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  52. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  53.  
  54.                         All Rights Reserved
  55.  
  56. Permission to use, copy, modify, and distribute this software and its 
  57. documentation for any purpose and without fee is hereby granted, 
  58. provided that the above copyright notice appear in all copies and that
  59. both that copyright notice and this permission notice appear in 
  60. supporting documentation, and that the names of Digital or MIT not be
  61. used in advertising or publicity pertaining to distribution of the
  62. software without specific, written prior permission.  
  63.  
  64. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  65. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  66. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  67. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  68. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  69. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  70. SOFTWARE.
  71.  
  72. ******************************************************************/
  73. /* $XConsortium: connection.c,v 1.120 89/11/13 11:53:33 rws Exp $ */
  74. /*****************************************************************
  75.  *  Stuff to create connections --- OS dependent
  76.  *
  77.  *      EstablishNewConnections, CreateWellKnownSockets, ResetWellKnownSockets,
  78.  *      CloseDownConnection, CheckConnections, AddEnabledDevice,
  79.  *    RemoveEnabledDevice, OnlyListToOneClient,
  80.  *      ListenToAllClients,
  81.  *
  82.  *      (WaitForSomething is in its own file)
  83.  *
  84.  *      In this implementation, a client socket table is not kept.
  85.  *      Instead, what would be the index into the table is just the
  86.  *      file descriptor of the socket.  This won't work for if the
  87.  *      socket ids aren't small nums (0 - 2^8)
  88.  *
  89.  *****************************************************************/
  90.  
  91. #include <dbm.h>
  92. #undef NULL
  93. #include "X.h"
  94. #include "Xproto.h"
  95. #include <sys/param.h>
  96. #include <errno.h>
  97. #include "Xos.h"            /* for strings, file, time */
  98. #include <sys/socket.h>
  99.  
  100. #include <signal.h>
  101. #include <fcntl.h>
  102. #include <setjmp.h>
  103.  
  104. #ifdef hpux
  105. #include <sys/utsname.h>
  106. #include <sys/ioctl.h>
  107. #endif
  108.  
  109. #ifdef TCPCONN
  110. # include <netinet/in.h>
  111. # ifndef hpux
  112. #  ifdef apollo
  113. #   ifndef NO_TCP_H
  114. #    include <netinet/tcp.h>
  115. #   endif
  116. #  else
  117. #   include <netinet/tcp.h>
  118. #  endif
  119. # endif
  120. #endif
  121.  
  122. #ifdef UNIXCONN
  123. /*
  124.  * sites should be careful to have separate /tmp directories for diskless nodes
  125.  */
  126. #include <sys/un.h>
  127. #include <sys/stat.h>
  128. static int unixDomainConnection = -1;
  129. #endif
  130.  
  131. #ifdef SPRITEPDEVCONN
  132.  
  133. static int spritePdevConnection = -1;
  134. #endif 
  135.  
  136. #include <stdio.h>
  137. #include <sys/uio.h>
  138. #include "osstruct.h"
  139. #include "osdep.h"
  140. #include "opaque.h"
  141. #include "dixstruct.h"
  142.  
  143. #ifdef DNETCONN
  144. #include <netdnet/dn.h>
  145. #endif /* DNETCONN */
  146.  
  147. typedef long CCID;      /* mask of indices into client socket table */
  148.  
  149. #ifndef X_UNIX_PATH
  150. #ifdef hpux
  151. #define X_UNIX_DIR    "/usr/spool/sockets/X11"
  152. #define X_UNIX_PATH    "/usr/spool/sockets/X11/"
  153. #define OLD_UNIX_DIR    "/tmp/.X11-unix"
  154. #else
  155. #define X_UNIX_DIR    "/tmp/.X11-unix"
  156. #define X_UNIX_PATH    "/tmp/.X11-unix/X"
  157. #endif
  158. #endif
  159.  
  160. char *display;            /* The display number */
  161. int lastfdesc;            /* maximum file descriptor */
  162.  
  163. long WellKnownConnections;    /* Listener mask */
  164. long EnabledDevices[mskcnt];    /* mask for input devices that are on */
  165. long AllSockets[mskcnt];    /* select on this */
  166. long AllClients[mskcnt];    /* available clients */
  167. long LastSelectMask[mskcnt];    /* mask returned from last select call */
  168. long ClientsWithInput[mskcnt];    /* clients with FULL requests in buffer */
  169. long ClientsWriteBlocked[mskcnt];/* clients who cannot receive output */
  170. long OutputPending[mskcnt];    /* clients with reply/event data ready to go */
  171. long MaxClients = MAXSOCKS ;
  172. long OutputBufferSize = BUFSIZ; /* output buffer size (must be > 0) */
  173. long NConnBitArrays = mskcnt;
  174. Bool NewOutputPending;        /* not yet attempted to write some new output */
  175. Bool AnyClientsWriteBlocked;    /* true if some client blocked on write */
  176.  
  177. static Bool SendSignal;        /* send SIGUSR1 to parent process */
  178. static int ParentProcess;
  179.  
  180. static Bool debug_conns = FALSE;
  181.  
  182. static int SavedAllClients[mskcnt];
  183. static int SavedAllSockets[mskcnt];
  184. static int SavedClientsWithInput[mskcnt];
  185. static int GrabInProgress = 0;
  186.  
  187. int ConnectionTranslation[MAXSOCKS];
  188. extern ClientPtr NextAvailableClient();
  189.  
  190. extern int AutoResetServer();
  191. extern int GiveUp();
  192. extern XID CheckAuthorization();
  193. static void CloseDownFileDescriptor(), ErrorConnMax();
  194. extern void FreeOsBuffers(), ResetOsBuffers();
  195.  
  196. #ifdef XDMCP
  197. void XdmcpOpenDisplay(), XdmcpInit(), XdmcpReset(), XdmcpCloseDisplay();
  198. #endif
  199.  
  200. #ifdef TCPCONN
  201. static int
  202. open_tcp_socket ()
  203. {
  204.     struct sockaddr_in insock;
  205.     int request;
  206.     int retry;
  207. #ifndef SO_DONTLINGER
  208. #ifdef SO_LINGER
  209.     static int linger[2] = { 0, 0 };
  210. #endif /* SO_LINGER */
  211. #endif /* SO_DONTLINGER */
  212.  
  213. #if defined(AIX) && defined(IBM_OS_HAS_HFT) && !defined(IBM_OS_HAS_X_QUEUE)
  214. #ifndef FORCE_DISPLAY_NUM
  215.     extern int AIXTCPSocket;
  216.     if (AIXTCPSocket>=0) {
  217.         request= AIXTCPSocket;
  218.     } else
  219. #endif /* FORCE_DISPLAY_NUM */
  220. #endif /* AIX && etc. */
  221.     if ((request = socket (AF_INET, SOCK_STREAM, 0)) < 0) 
  222.     {
  223.     Error ("Creating TCP socket");
  224.     return -1;
  225.     } 
  226. #ifdef SO_REUSEADDR
  227.     /* Necesary to restart the server without a reboot */
  228. #ifdef hpux
  229.     set_socket_option (request, SO_REUSEADDR);
  230. #else
  231.     {
  232.     int one = 1;
  233.     setsockopt(request, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
  234.     }
  235. #endif
  236. #endif /* SO_REUSEADDR */
  237. #if defined(AIX) && defined(IBM_OS_HAS_HFT) && !defined(IBM_OS_HAS_X_QUEUE)
  238. #ifndef FORCE_DISPLAY_NUMBER
  239.     if (AIXTCPSocket<0)
  240. #endif
  241. #endif
  242.     {
  243.     bzero ((char *)&insock, sizeof (insock));
  244.     insock.sin_family = AF_INET;
  245.     insock.sin_port = htons ((unsigned short)(X_TCP_PORT + atoi (display)));
  246.     insock.sin_addr.s_addr = htonl(INADDR_ANY);
  247.     retry = 20;
  248.     while (bind(request, (struct sockaddr *) &insock, sizeof (insock)))
  249.     {
  250.     if (--retry == 0) {
  251.         Error ("Binding TCP socket");
  252.         close (request);
  253.         return -1;
  254.     }
  255. #ifdef SO_REUSEADDR
  256.     sleep (1);
  257. #else
  258.     sleep (10);
  259. #endif /* SO_REUSEDADDR */
  260.     }
  261.     }
  262. #ifdef SO_DONTLINGER
  263.     if(setsockopt (request, SOL_SOCKET, SO_DONTLINGER, (char *)NULL, 0))
  264.     Error ("Setting TCP SO_DONTLINGER");
  265. #else
  266. #ifdef SO_LINGER
  267.     if(setsockopt (request, SOL_SOCKET, SO_LINGER,
  268.            (char *)linger, sizeof(linger)))
  269.     Error ("Setting TCP SO_LINGER");
  270. #endif /* SO_LINGER */
  271. #endif /* SO_DONTLINGER */
  272.     if (listen (request, 5)) {
  273.     Error ("TCP Listening");
  274.     close (request);
  275.     return -1;
  276.     }
  277.     return request;
  278. }
  279. #endif /* TCPCONN */
  280.  
  281. #ifdef UNIXCONN
  282.  
  283. static struct sockaddr_un unsock;
  284.  
  285. static int
  286. open_unix_socket ()
  287. {
  288.     int oldUmask;
  289.     int request;
  290.  
  291.     unsock.sun_family = AF_UNIX;
  292.     oldUmask = umask (0);
  293. #ifdef X_UNIX_DIR
  294.     mkdir (X_UNIX_DIR, 0777);
  295.     chmod (X_UNIX_DIR, 0777);
  296. #endif
  297.     strcpy (unsock.sun_path, X_UNIX_PATH);
  298.     strcat (unsock.sun_path, display);
  299. #ifdef hpux
  300.     {  
  301.         /*    The following is for backwards compatibility
  302.          *    with old HP clients. This old scheme predates the use
  303.      *    of the /usr/spool/sockets directory, and uses hostname:display
  304.       *    in the /tmp/.X11-unix directory
  305.          */
  306.         struct utsname systemName;
  307.     static char oldLinkName[256];
  308.  
  309.         uname(&systemName);
  310.         strcpy(oldLinkName, OLD_UNIX_DIR);
  311.         mkdir(oldLinkName, 0777);
  312.         chown(oldLinkName, 2, 3);
  313.         strcat(oldLinkName, "/");
  314.         strcat(oldLinkName, systemName.nodename);
  315.         strcat(oldLinkName, display);
  316.         unlink(oldLinkName);
  317.         symlink(unsock.sun_path, oldLinkName);
  318.     }
  319. #endif    /* hpux */
  320.     unlink (unsock.sun_path);
  321.     if ((request = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) 
  322.     {
  323.     Error ("Creating Unix socket");
  324.     return -1;
  325.     } 
  326.     if (bind(request, (struct sockaddr *)&unsock, strlen(unsock.sun_path)+2))
  327.     {
  328.     Error ("Binding Unix socket");
  329.     close (request);
  330.     return -1;
  331.     }
  332.     if (listen (request, 5))
  333.     {
  334.     Error ("Unix Listening");
  335.     close (request);
  336.     return -1;
  337.     }
  338.     (void)umask(oldUmask);
  339.     return request;
  340. }
  341. #endif /*UNIXCONN */
  342.  
  343. #ifdef SPRITEPDEVCONN
  344.  
  345. /*
  346.  * Template for the pseudo-device through which we communicate. Given to
  347.  * one of the printf functions and expects two arguments: the name of the
  348.  * local host and the display number we're using.
  349.  */
  350. #define    DEVICE_TEMPLATE    "/hosts/%s/X%s"
  351.  
  352. static char deviceName[1024];
  353.  
  354. static int
  355. open_sprite_pdev ()
  356. {
  357.     int oldUmask;
  358.     int request;
  359.     char hostname[256];
  360.  
  361.     /*
  362.      * Create the pseudo-device, making sure it's readable and writable
  363.      * by everyone.
  364.      */
  365.  
  366.     gethostname(hostname, 256);
  367.     hostname[255] = 0;
  368.     sprintf (deviceName, DEVICE_TEMPLATE, hostname, display);
  369.     oldUmask = umask (0);
  370.     request = PdevCreate(deviceName);
  371.     (void)umask(oldUmask);
  372.     return request;
  373. }
  374. #endif /* SPRITEPDEVCONN */
  375.  
  376. #ifdef hpux
  377. /*
  378.  * hpux returns EOPNOTSUPP when using getpeername on a unix-domain
  379.  * socket.  In this case, smash the socket address with the address
  380.  * used to bind the connection socket and return success.
  381.  */
  382. hpux_getpeername(fd, from, fromlen)
  383.     int    fd;
  384.     struct sockaddr *from;
  385.     int            *fromlen;
  386. {
  387.     int        ret;
  388.     int        len;
  389.  
  390.     ret = getpeername(fd, from, fromlen);
  391.     if (ret == -1 && errno == EOPNOTSUPP)
  392.     {
  393.     ret = 0;
  394.     len = strlen(unsock.sun_path)+2;
  395.     if (len > *fromlen)
  396.         len = *fromlen;
  397.     bcopy ((char *) &unsock, (char *) from, len);
  398.     *fromlen = len;
  399.     }
  400.     return ret;
  401. }
  402.  
  403. #define getpeername(fd, from, fromlen)    hpux_getpeername(fd, from, fromlen)
  404.  
  405. #endif
  406.  
  407. #ifdef DNETCONN
  408. static int
  409. open_dnet_socket ()
  410. {
  411.     int request;
  412.     struct sockaddr_dn dnsock;
  413.  
  414.     if ((request = socket (AF_DECnet, SOCK_STREAM, 0)) < 0) 
  415.     {
  416.     Error ("Creating DECnet socket");
  417.     return -1;
  418.     } 
  419.     bzero ((char *)&dnsock, sizeof (dnsock));
  420.     dnsock.sdn_family = AF_DECnet;
  421.     sprintf(dnsock.sdn_objname, "X$X%d", atoi (display));
  422.     dnsock.sdn_objnamel = strlen(dnsock.sdn_objname);
  423.     if (bind (request, (struct sockaddr *) &dnsock, sizeof (dnsock)))
  424.     {
  425.     Error ("Binding DECnet socket");
  426.     close (request);
  427.     return -1;
  428.     }
  429.     if (listen (request, 5))
  430.     {
  431.     Error ("DECnet Listening");
  432.     close (request);
  433.     return -1;
  434.     }
  435.     return request;
  436. }
  437. #endif /* DNETCONN */
  438.  
  439. /*****************
  440.  * CreateWellKnownSockets
  441.  *    At initialization, create the sockets to listen on for new clients.
  442.  *****************/
  443.  
  444. void
  445. CreateWellKnownSockets()
  446. {
  447.     int        request, i;
  448.  
  449.     CLEARBITS(AllSockets);
  450.     CLEARBITS(AllClients);
  451.     CLEARBITS(LastSelectMask);
  452.     CLEARBITS(ClientsWithInput);
  453.  
  454.     for (i=0; i<MAXSOCKS; i++) ConnectionTranslation[i] = 0;
  455.     
  456. #ifdef    hpux
  457.     lastfdesc = _NFILE - 1;
  458. #else
  459.     lastfdesc = getdtablesize() - 1;
  460. #endif    /* hpux */
  461.  
  462.     if (lastfdesc > MAXSOCKS)
  463.     {
  464.     lastfdesc = MAXSOCKS;
  465.     if (debug_conns)
  466.         ErrorF( "GOT TO END OF SOCKETS %d\n", MAXSOCKS);
  467.     }
  468.  
  469.     WellKnownConnections = 0;
  470. #ifdef TCPCONN
  471.     if ((request = open_tcp_socket ()) != -1) {
  472.     WellKnownConnections |= (1L << request);
  473.     DefineSelf (request);
  474.     }
  475. #endif /* TCPCONN */
  476. #ifdef UNIXCONN
  477.     if ((request = open_unix_socket ()) != -1) {
  478.     WellKnownConnections |= (1L << request);
  479.     unixDomainConnection = request;
  480.     }
  481. #endif /* UNIXCONN */
  482. #ifdef SPRITEPDEVCONN
  483.     if ((request = open_sprite_pdev ()) != -1) {
  484.     WellKnownConnections |= (1L << request);
  485.     spritePdevConnection = request;
  486.     }
  487. #endif /* SPRITEPDEVCONN */
  488. #ifdef DNETCONN
  489.     if ((request = open_dnet_socket ()) != -1) {
  490.     WellKnownConnections |= (1L << request);
  491.     DefineSelf (request);
  492.     }
  493. #endif /* DNETCONN */
  494.     if (WellKnownConnections == 0)
  495.         FatalError ("Cannot establish any listening sockets");
  496.     signal (SIGPIPE, SIG_IGN);
  497.     signal (SIGHUP, AutoResetServer);
  498.     signal (SIGINT, GiveUp);
  499.     signal (SIGTERM, GiveUp);
  500.     AllSockets[0] = WellKnownConnections;
  501.     ResetHosts(display);
  502.     /*
  503.      * Magic:  If SIGUSR1 was set to SIG_IGN when
  504.      * the server started, assume that either
  505.      *
  506.      *  a- The parent process is ignoring SIGUSR1
  507.      *
  508.      * or
  509.      *
  510.      *  b- The parent process is expecting a SIGUSR1
  511.      *     when the server is ready to accept connections
  512.      *
  513.      * In the first case, the signal will be harmless,
  514.      * in the second case, the signal will be quite
  515.      * useful
  516.      */
  517.     if (signal (SIGUSR1, SIG_IGN) == SIG_IGN)
  518.     SendSignal = TRUE;
  519.     ParentProcess = getppid ();
  520.     if (SendSignal) {
  521.     if (ParentProcess > 0) {
  522.         kill (ParentProcess, SIGUSR1);
  523.     }
  524.     }
  525. #ifdef XDMCP
  526.     XdmcpInit ();
  527. #endif
  528. }
  529.  
  530. void
  531. ResetWellKnownSockets ()
  532. {
  533.     ResetOsBuffers();
  534. #ifdef UNIXCONN
  535.     if (unixDomainConnection != -1)
  536.     {
  537.     /*
  538.      * see if the unix domain socket has disappeared
  539.      */
  540.     struct stat    statb;
  541.  
  542.     if (stat (unsock.sun_path, &statb) == -1 ||
  543.         (statb.st_mode & S_IFMT) != S_IFSOCK)
  544.     {
  545.         ErrorF ("Unix domain socket %s trashed, recreating\n",
  546.         unsock.sun_path);
  547.         (void) unlink (unsock.sun_path);
  548.         (void) close (unixDomainConnection);
  549.         WellKnownConnections &= ~(1L << unixDomainConnection);
  550.         unixDomainConnection = open_unix_socket ();
  551.         if (unixDomainConnection != -1)
  552.         WellKnownConnections |= (1L << unixDomainConnection);
  553.     }
  554.     }
  555. #endif /* UNIXCONN */
  556.     ResetAuthorization ();
  557.     ResetHosts(display);
  558.     /*
  559.      * See above in CreateWellKnownSockets about SIGUSR1
  560.      */
  561.     if (SendSignal) {
  562.     if (ParentProcess > 0) {
  563.         kill (ParentProcess, SIGUSR1);
  564.     }
  565.     }
  566.     /*
  567.      * restart XDMCP
  568.      */
  569. #ifdef XDMCP
  570.     XdmcpReset ();
  571. #endif
  572. }
  573.  
  574. /*****************************************************************
  575.  * ClientAuthorized
  576.  *
  577.  *    Sent by the client at connection setup:
  578.  *                typedef struct _xConnClientPrefix {
  579.  *                   CARD8    byteOrder;
  580.  *                   BYTE    pad;
  581.  *                   CARD16    majorVersion, minorVersion;
  582.  *                   CARD16    nbytesAuthProto;    
  583.  *                   CARD16    nbytesAuthString;   
  584.  *                 } xConnClientPrefix;
  585.  *
  586.  *         It is hoped that eventually one protocol will be agreed upon.  In the
  587.  *        mean time, a server that implements a different protocol than the
  588.  *        client expects, or a server that only implements the host-based
  589.  *        mechanism, will simply ignore this information.
  590.  *
  591.  *****************************************************************/
  592.  
  593. char * 
  594. ClientAuthorized(client, proto_n, auth_proto, string_n, auth_string)
  595.     ClientPtr client;
  596.     char *auth_proto, *auth_string;
  597.     unsigned short proto_n, string_n;
  598. {
  599.     register OsCommPtr priv;
  600.     union {
  601.     struct sockaddr sa;
  602. #ifdef UNIXCONN
  603.     struct sockaddr_un un;
  604. #endif /* UNIXCONN */
  605. #ifdef TCPCONN
  606.     struct sockaddr_in in;
  607. #endif /* TCPCONN */
  608. #ifdef DNETCONN
  609.     struct sockaddr_dn dn;
  610. #endif /* DNETCONN */
  611.     } from;
  612.     int    fromlen = sizeof (from);
  613.     XID     auth_id;
  614.  
  615.     auth_id = CheckAuthorization (proto_n, auth_proto,
  616.                   string_n, auth_string);
  617.  
  618.     priv = (OsCommPtr)client->osPrivate;
  619. #ifdef SPRITEPDEVCONN
  620.     if (auth_id == (XID) ~0L &&
  621.     !InvalidPdevConn (priv->fd)) {
  622.     auth_id = (XID) 0;
  623.     }
  624. #endif
  625.     if (auth_id == (XID) ~0L && 
  626.     getpeername (priv->fd, &from.sa, &fromlen) != -1 &&
  627.         !InvalidHost (&from.sa, fromlen))
  628.     {
  629.     auth_id = (XID) 0;
  630.     }
  631.  
  632.     if (auth_id == (XID) ~0L)
  633.     return "Client is not authorized to connect to Server";
  634.  
  635.     priv->auth_id = auth_id;
  636.     priv->conn_time = 0;
  637.  
  638.     /* At this point, if the client is authorized to change the access control
  639.      * list, we should getpeername() information, and add the client to
  640.      * the selfhosts list.  It's not really the host machine, but the
  641.      * true purpose of the selfhosts list is to see who may change the
  642.      * access control list.
  643.      */
  644.     return((char *)NULL);
  645. }
  646.  
  647. /*****************
  648.  * EstablishNewConnections
  649.  *    If anyone is waiting on listened sockets, accept them.
  650.  *    Returns a mask with indices of new clients.  Updates AllClients
  651.  *    and AllSockets.
  652.  *****************/
  653.  
  654. void
  655. EstablishNewConnections()
  656. {
  657.     long readyconnections;     /* mask of listeners that are ready */
  658.     int curconn;                  /* fd of listener that's ready */
  659.     register int newconn;         /* fd of new client */
  660.     long connect_time;
  661.     register int i;
  662.     register ClientPtr client;
  663.     register OsCommPtr oc;
  664.  
  665. #ifdef TCP_NODELAY
  666.     union {
  667.     struct sockaddr sa;
  668. #ifdef UNIXCONN
  669.     struct sockaddr_un un;
  670. #endif /* UNIXCONN */
  671. #ifdef TCPCONN
  672.     struct sockaddr_in in;
  673. #endif /* TCPCONN */
  674. #ifdef DNETCONN
  675.     struct sockaddr_dn dn;
  676. #endif /* DNETCONN */
  677.     } from;
  678.     int    fromlen;
  679. #endif /* TCP_NODELAY */
  680.  
  681.     readyconnections = (LastSelectMask[0] & WellKnownConnections);
  682.     if (!readyconnections)
  683.     return;
  684.     connect_time = GetTimeInMillis();
  685.     /* kill off stragglers */
  686.     for (i=1; i<currentMaxClients; i++)
  687.     {
  688.     if (client = clients[i])
  689.     {
  690.         oc = (OsCommPtr)(client->osPrivate);
  691.         if (oc && (oc->conn_time != 0) &&
  692.         (connect_time - oc->conn_time) >= TimeOutValue)
  693.         CloseDownClient(client);     
  694.     }
  695.     }
  696.     while (readyconnections) 
  697.     {
  698.     curconn = ffs (readyconnections) - 1;
  699.     readyconnections &= ~(1 << curconn);
  700. #ifdef SPRITEPDEVCONN
  701.     if (PdevIsMaster(curconn)) {
  702.         if ((newconn = PdevAccept(curconn)) < 0) 
  703.         continue;
  704.     } else
  705. #endif
  706.     if ((newconn = accept (curconn,
  707.                   (struct sockaddr *) NULL, 
  708.                   (int *)NULL)) < 0) 
  709.         continue;
  710. #ifdef TCP_NODELAY
  711.     fromlen = sizeof (from);
  712.     if (!getpeername (newconn, &from.sa, &fromlen))
  713.     {
  714.         if (fromlen && (from.sa.sa_family == AF_INET)) 
  715.         {
  716.         int mi = 1;
  717.         setsockopt (newconn, IPPROTO_TCP, TCP_NODELAY,
  718.                (char *)&mi, sizeof (int));
  719.         }
  720.     }
  721. #endif /* TCP_NODELAY */
  722. #ifdef    hpux
  723.     /*
  724.      * HPUX does not have  FNDELAY
  725.      */
  726.     {
  727.         int    arg;
  728.         arg = 1;
  729.         ioctl(newconn, FIOSNBIO, &arg);
  730.     }
  731. #else
  732.     fcntl (newconn, F_SETFL, FNDELAY);
  733. #endif /* hpux */
  734.     oc = (OsCommPtr)xalloc(sizeof(OsCommRec));
  735.     if (!oc)
  736.     {
  737.         xfree(oc);
  738.         ErrorConnMax(newconn);
  739.         close(newconn);
  740.         continue;
  741.     }
  742.     if (GrabInProgress)
  743.     {
  744.         BITSET(SavedAllClients, newconn);
  745.         BITSET(SavedAllSockets, newconn);
  746.     }
  747.     else
  748.     {
  749.         BITSET(AllClients, newconn);
  750.         BITSET(AllSockets, newconn);
  751.     }
  752.     oc->fd = newconn;
  753.     oc->input = (ConnectionInputPtr)NULL;
  754.     oc->output = (ConnectionOutputPtr)NULL;
  755.     oc->conn_time = connect_time;
  756.     if ((newconn < lastfdesc) &&
  757.         (client = NextAvailableClient((pointer)oc)))
  758.     {
  759.         ConnectionTranslation[newconn] = client->index;
  760.     }
  761.     else
  762.     {
  763.         ErrorConnMax(newconn);
  764.         CloseDownFileDescriptor(oc);
  765.     }
  766. #ifdef XDMCP
  767.     /* indicate to Xdmcp protocol that we've opened new client */
  768.     XdmcpOpenDisplay(newconn);
  769. #endif /* XDMCP */
  770.     }
  771. }
  772.  
  773. #define NOROOM "Maximum number of clients reached"
  774.  
  775. /************
  776.  *   ErrorConnMax
  777.  *     Fail a connection due to lack of client or file descriptor space
  778.  ************/
  779.  
  780. static void
  781. ErrorConnMax(fd)
  782.     register int fd;
  783. {
  784.     xConnSetupPrefix csp;
  785.     char pad[3];
  786.     struct iovec iov[3];
  787.     char byteOrder = 0;
  788.     int whichbyte = 1;
  789.     struct timeval waittime;
  790.     long mask[mskcnt];
  791.  
  792.     /* if these seems like a lot of trouble to go to, it probably is */
  793.     waittime.tv_sec = BOTIMEOUT / MILLI_PER_SECOND;
  794.     waittime.tv_usec = (BOTIMEOUT % MILLI_PER_SECOND) *
  795.                (1000000 / MILLI_PER_SECOND);
  796.     CLEARBITS(mask);
  797.     BITSET(mask, fd);
  798.     (void)select(fd + 1, (int *) mask, (int *) NULL, (int *) NULL, &waittime);
  799.     /* try to read the byte-order of the connection */
  800. #ifdef SPRITEPDEVCONN
  801.     if (PdevIsPdevConn(fd)) {
  802.     (void)read(fd, &byteOrder, 1);
  803.     } else 
  804. #endif
  805.     (void)read(fd, &byteOrder, 1);
  806.     if ((byteOrder == 'l') || (byteOrder == 'B'))
  807.     {
  808.     csp.success = xFalse;
  809.     csp.lengthReason = sizeof(NOROOM) - 1;
  810.     csp.length = (sizeof(NOROOM) + 2) >> 2;
  811.     csp.majorVersion = X_PROTOCOL;
  812.     csp.minorVersion = X_PROTOCOL_REVISION;
  813.     if (((*(char *) &whichbyte) && (byteOrder == 'B')) ||
  814.         (!(*(char *) &whichbyte) && (byteOrder == 'l')))
  815.     {
  816.         swaps(&csp.majorVersion, whichbyte);
  817.         swaps(&csp.minorVersion, whichbyte);
  818.         swaps(&csp.length, whichbyte);
  819.     }
  820.     iov[0].iov_len = sz_xConnSetupPrefix;
  821.     iov[0].iov_base = (char *) &csp;
  822.     iov[1].iov_len = csp.lengthReason;
  823.     iov[1].iov_base = NOROOM;
  824.     iov[2].iov_len = (4 - (csp.lengthReason & 3)) & 3;
  825.     iov[2].iov_base = pad;
  826. #ifdef SPRITEPDEVCONN
  827.     if (PdevIsPdevConn(fd)) {
  828.         (void)PdevWritev(fd, iov, 3);
  829.     } else
  830. #endif
  831.     (void)writev(fd, iov, 3);
  832.     }
  833. }
  834.  
  835. /************
  836.  *   CloseDownFileDescriptor:
  837.  *     Remove this file descriptor and it's I/O buffers, etc.
  838.  ************/
  839.  
  840. static void
  841. CloseDownFileDescriptor(oc)
  842.     register OsCommPtr oc;
  843. {
  844.     int connection = oc->fd;
  845.  
  846. #ifdef SPRITEPDEVCONN
  847.     if (PdevIsPdevConn(connection)) {
  848.     PdevClose(connection);
  849.     } else
  850. #endif
  851.     close(connection);
  852.     FreeOsBuffers(oc);
  853.     BITCLEAR(AllSockets, connection);
  854.     BITCLEAR(AllClients, connection);
  855.     BITCLEAR(ClientsWithInput, connection);
  856.     if (GrabInProgress)
  857.     {
  858.     BITCLEAR(SavedAllSockets, connection);
  859.     BITCLEAR(SavedAllClients, connection);
  860.     BITCLEAR(SavedClientsWithInput, connection);
  861.     }
  862.     BITCLEAR(ClientsWriteBlocked, connection);
  863.     if (!ANYSET(ClientsWriteBlocked))
  864.         AnyClientsWriteBlocked = FALSE;
  865.     BITCLEAR(OutputPending, connection);
  866.     xfree(oc);
  867. }
  868.  
  869. /*****************
  870.  * CheckConections
  871.  *    Some connection has died, go find which one and shut it down 
  872.  *    The file descriptor has been closed, but is still in AllClients.
  873.  *    If would truly be wonderful if select() would put the bogus
  874.  *    file descriptors in the exception mask, but nooooo.  So we have
  875.  *    to check each and every socket individually.
  876.  *****************/
  877.  
  878. void
  879. CheckConnections()
  880. {
  881.     long        mask;
  882.     long        tmask[mskcnt]; 
  883.     register int    curclient, curoff;
  884.     int            i;
  885.     struct timeval    notime;
  886.     int r;
  887.  
  888.     notime.tv_sec = 0;
  889.     notime.tv_usec = 0;
  890.  
  891.     for (i=0; i<mskcnt; i++)
  892.     {
  893.     mask = AllClients[i];
  894.         while (mask)
  895.         {
  896.         curoff = ffs (mask) - 1;
  897.          curclient = curoff + (i << 5);
  898.             CLEARBITS(tmask);
  899.             BITSET(tmask, curclient);
  900.             r = select (curclient + 1, (int *)tmask, (int *)NULL, (int *)NULL, 
  901.             ¬ime);
  902.             if (r < 0)
  903.         CloseDownClient(clients[ConnectionTranslation[curclient]]);
  904.         mask &= ~(1 << curoff);
  905.     }
  906.     }    
  907. }
  908.  
  909.  
  910. /*****************
  911.  * CloseDownConnection
  912.  *    Delete client from AllClients and free resources 
  913.  *****************/
  914.  
  915. CloseDownConnection(client)
  916.     ClientPtr client;
  917. {
  918.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  919.  
  920.     if (oc->output && oc->output->count)
  921.     FlushClient(client, oc, (char *)NULL, 0);
  922.     ConnectionTranslation[oc->fd] = 0;
  923. #ifdef XDMCP
  924.     XdmcpCloseDisplay(oc->fd);
  925. #endif
  926.     CloseDownFileDescriptor(oc);
  927.     client->osPrivate = (pointer)NULL;
  928. }
  929.  
  930.  
  931. AddEnabledDevice(fd)
  932.     int fd;
  933. {
  934.     BITSET(EnabledDevices, fd);
  935.     BITSET(AllSockets, fd);
  936. }
  937.  
  938.  
  939. RemoveEnabledDevice(fd)
  940.     int fd;
  941. {
  942.     BITCLEAR(EnabledDevices, fd);
  943.     BITCLEAR(AllSockets, fd);
  944. }
  945.  
  946. /*****************
  947.  * OnlyListenToOneClient:
  948.  *    Only accept requests from  one client.  Continue to handle new
  949.  *    connections, but don't take any protocol requests from the new
  950.  *    ones.  Note that if GrabInProgress is set, EstablishNewConnections
  951.  *    needs to put new clients into SavedAllSockets and SavedAllClients.
  952.  *    Note also that there is no timeout for this in the protocol.
  953.  *    This routine is "undone" by ListenToAllClients()
  954.  *****************/
  955.  
  956. OnlyListenToOneClient(client)
  957.     ClientPtr client;
  958. {
  959.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  960.     int connection = oc->fd;
  961.  
  962.     if (! GrabInProgress)
  963.     {
  964.     COPYBITS (ClientsWithInput, SavedClientsWithInput);
  965.         BITCLEAR (SavedClientsWithInput, connection);
  966.     if (GETBIT(ClientsWithInput, connection))
  967.     {
  968.         CLEARBITS(ClientsWithInput);        
  969.         BITSET(ClientsWithInput, connection);
  970.     }
  971.     else
  972.         {
  973.         CLEARBITS(ClientsWithInput);        
  974.     }
  975.     COPYBITS(AllSockets, SavedAllSockets);
  976.     COPYBITS(AllClients, SavedAllClients);
  977.  
  978.     UNSETBITS(AllSockets, AllClients);
  979.     BITSET(AllSockets, connection);
  980.     CLEARBITS(AllClients);
  981.     BITSET(AllClients, connection);
  982.     GrabInProgress = client->index;
  983.     }
  984. }
  985.  
  986. /****************
  987.  * ListenToAllClients:
  988.  *    Undoes OnlyListentToOneClient()
  989.  ****************/
  990.  
  991. ListenToAllClients()
  992. {
  993.     if (GrabInProgress)
  994.     {
  995.     ORBITS(AllSockets, AllSockets, SavedAllSockets);
  996.     ORBITS(AllClients, AllClients, SavedAllClients);
  997.     ORBITS(ClientsWithInput, ClientsWithInput, SavedClientsWithInput);
  998.     GrabInProgress = 0;
  999.     }    
  1000. }
  1001.  
  1002. /****************
  1003.  * IgnoreClient
  1004.  *    Removes one client from input masks.
  1005.  *    Must have cooresponding call to AttendClient.
  1006.  ****************/
  1007.  
  1008. static long IgnoredClientsWithInput[mskcnt];
  1009. static long IgnoredSavedClientsWithInput[mskcnt];
  1010.  
  1011. IgnoreClient (client)
  1012.     ClientPtr    client;
  1013. {
  1014.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  1015.     int connection = oc->fd;
  1016.  
  1017.     if (GETBIT (ClientsWithInput, connection))
  1018.     BITSET(IgnoredClientsWithInput, connection);
  1019.     else
  1020.     BITCLEAR(IgnoredClientsWithInput, connection);
  1021.     BITCLEAR(ClientsWithInput, connection);
  1022.     BITCLEAR(AllSockets, connection);
  1023.     BITCLEAR(AllClients, connection);
  1024.     if (GrabInProgress)
  1025.     {
  1026.         if (GETBIT (SavedClientsWithInput, connection))
  1027.         BITSET(IgnoredSavedClientsWithInput, connection);
  1028.         else
  1029.         BITCLEAR(IgnoredSavedClientsWithInput, connection);
  1030.     BITCLEAR(SavedClientsWithInput, connection);
  1031.     BITCLEAR(SavedAllSockets, connection);
  1032.     BITCLEAR(SavedAllClients, connection);
  1033.     }
  1034. }
  1035.  
  1036. /****************
  1037.  * AttendClient
  1038.  *    Adds one client back into the input masks.
  1039.  ****************/
  1040.  
  1041. AttendClient (client)
  1042.     ClientPtr    client;
  1043. {
  1044.     OsCommPtr oc = (OsCommPtr)client->osPrivate;
  1045.     int connection = oc->fd;
  1046.  
  1047.     if (!GrabInProgress || GrabInProgress == client->index)
  1048.     {
  1049.         BITSET(AllClients, connection);
  1050.         BITSET(AllSockets, connection);
  1051.         if (GETBIT (IgnoredClientsWithInput, connection))
  1052.         BITSET(ClientsWithInput, connection);
  1053.     }
  1054.     else
  1055.     {
  1056.     BITSET(SavedAllClients, connection);
  1057.     BITSET(SavedAllSockets, connection);
  1058.     if (GETBIT(IgnoredSavedClientsWithInput, connection))
  1059.         BITSET(SavedClientsWithInput, connection);
  1060.     }
  1061. }
  1062.  
  1063. #if defined(AIX) && defined(IBM_OS_HAS_HFT) && !defined(IBM_OS_HAS_X_QUEUE)
  1064.  
  1065. static int grabbingClient;
  1066. static int reallyGrabbed;
  1067.  
  1068. /****************
  1069. * DontListenToAnybody:
  1070. *   Don't listen to requests from any clients. Continue to handle new
  1071. *   connections, but don't take any protocol requests from anybody.
  1072. *   We have to take care if there is already a grab in progress, though.
  1073. *   Undone by PayAttentionToClientsAgain. We also have to be careful
  1074. *   not to accept any more input from the currently dispatched client.
  1075. *   we do this be telling dispatch it is time to yield.
  1076.  
  1077. *   We call this when the server loses access to the glass
  1078. *   (user hot-keys away).  This looks like a grab by the 
  1079. *   server itself, but gets a little tricky if there is already
  1080. *   a grab in progress.
  1081. ******************/
  1082.  
  1083. void
  1084. DontListenToAnybody()
  1085. {
  1086.     if (!GrabInProgress) {
  1087.     COPYBITS(ClientsWithInput, SavedClientsWithInput);
  1088.     COPYBITS(AllSockets, SavedAllSockets);
  1089.     COPYBITS(AllClients, SavedAllClients);
  1090.     GrabInProgress = TRUE;
  1091.     reallyGrabbed = FALSE;
  1092.     } else {
  1093.     reallyGrabbed = TRUE;
  1094.     }
  1095.     CLEARBITS(ClientsWithInput);
  1096.     UNSETBITS(AllSockets, AllClients);
  1097.     CLEARBITS(AllClients);
  1098.     isItTimeToYield = TRUE;
  1099. }
  1100.  
  1101. void
  1102. PayAttentionToClientsAgain()
  1103. {
  1104.     if (reallyGrabbed) {
  1105.     BITSET(AllSockets, grabbingClient);
  1106.     BITSET(AllClients, grabbingClient);
  1107.     } else {
  1108.     ListenToAllClients();
  1109.     }
  1110.     reallyGrabbed = FALSE;
  1111. }
  1112.  
  1113. #endif
  1114. @
  1115.  
  1116.  
  1117. 1.5
  1118. log
  1119. @don't open tcp sockets if we are going to use the pseudo-device
  1120. @
  1121. text
  1122. @d421 1
  1123. a421 1
  1124. #if defined(TCPCONN) && !defined(SPRITEPDEVCONN)
  1125. @
  1126.  
  1127.  
  1128. 1.4
  1129. log
  1130. @put authorization support back in (addapted to pdevs)
  1131. @
  1132. text
  1133. @d421 1
  1134. a421 1
  1135. #ifdef TCPCONN
  1136. @
  1137.  
  1138.  
  1139. 1.3
  1140. log
  1141. @*** empty log message ***
  1142. @
  1143. text
  1144. @d507 1
  1145. d566 3
  1146. a568 1
  1147.     auth_id = (XID) ~0L;
  1148. d571 2
  1149. a572 1
  1150.     if (PdevIsPdevConn(priv->fd)) {
  1151. @
  1152.  
  1153.  
  1154. 1.2
  1155. log
  1156. @removed authorization hooks
  1157. @
  1158. text
  1159. @d82 5
  1160. d254 1
  1161. a254 1
  1162.       *    of the /usr/spool/sockets directory, and uses hostname:display
  1163. d294 33
  1164. d433 6
  1165. d497 1
  1166. a497 1
  1167.             unsock.sun_path);
  1168. a565 1
  1169.  
  1170. d567 5
  1171. d573 1
  1172. a573 1
  1173.        getpeername (priv->fd, &from.sa, &fromlen) != -1 &&
  1174. d647 6
  1175. d747 5
  1176. d773 5
  1177. d793 5
  1178. @
  1179.  
  1180.  
  1181. 1.1
  1182. log
  1183. @Initial revision
  1184. @
  1185. text
  1186. @a462 1
  1187.     ResetAuthorization ();
  1188. d521 1
  1189. a521 2
  1190.     auth_id = CheckAuthorization (proto_n, auth_proto,
  1191.                   string_n, auth_string);
  1192. @
  1193.